// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.notifications;

import org.dengues.commons.DenguesCommonsPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.UIJob;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-29 qiang.zhang $
 * 
 */
public abstract class AbstractNotificationUIJob extends UIJob {

    static final String PLUGIN_ID = DenguesCommonsPlugin.PLUGIN_ID;

    private AbstractNotificationsManager notificationsManager;

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractNotificationUiJob constructor comment.
     * 
     * @param name
     */
    public AbstractNotificationUIJob(String name) {
        super(name);
        setSystem(true);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractNotificationUiJob constructor comment.
     * 
     * @param jobDisplay
     * @param name
     */
    public AbstractNotificationUIJob(Display jobDisplay, String name) {
        super(jobDisplay, name);
        setSystem(true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public IStatus runInUIThread(IProgressMonitor monitor) {
        // NOTE: failure in one part of the initialization should
        // not prevent others
        try {
            notificationsManager = getNotificationsManager();
            notificationsManager.addNotificationProvider(getNotificationProvider());
            notificationsManager.startNotification(getNotificationDelay());
            getPreferenceStore().addPropertyChangeListener(notificationsManager);
        } catch (Throwable t) {
            return new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, t.getMessage(), t);
        }
        monitor.worked(1);
        return new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getNotificationProvider".
     * 
     * @return
     */
    protected abstract INotificationProvider getNotificationProvider();

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getNotificationDelay".
     * 
     * @return
     */
    protected abstract long getNotificationDelay();

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getPreferenceStore".
     * 
     * @return
     */
    protected abstract IPreferenceStore getPreferenceStore();

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getNotificationsManager".
     * 
     * @return
     */
    protected abstract AbstractNotificationsManager getNotificationsManager();
}
